﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ParticleFiltersDemo1
{
    public class ParticlesSet
    {
        double meanX; // координати на клъстера
        double meanY;
        public bool isDone = false; // ? сме си намерили координатите - (meanX, meanY)
        public Particle[] particles;
        public int Count
        {
            get
            {
                return particles.Length;
            }
         set; }

        public ParticlesSet( int count, Board board )
        {
            particles = new Particle[count];
            for ( int i = 0; i < count; i++ )
            {
                int[] res = board.randomFreeCell();
                Particle p = new Particle( ( double )res[0], ( double )res[1], 1, true, board );
                particles[i] = p;
            }

            ComputeMeanPoint( board );
        }

        //computing the coordinads of the mean point and if we've finished searching location
        //->meanX,meanY,isDone
        public void ComputeMeanPoint( Board board )
        {
            double weightSum = 0;
            double mean_x = 0;
            double mean_y = 0;
            for ( int i = 0; i < particles.Length; i++ )
            {
                Particle p = particles[i];
                weightSum += p.weight;
                mean_x += p.X * p.weight;
                mean_y += p.Y * p.weight;
            }
            if ( weightSum == 0 )
            {
                meanX = -1;
                meanY = -1;
                isDone = false;
                return;

            }
            mean_x /= weightSum;
            mean_y /= weightSum;
            int mCount = 0;
            for ( int i = 0; i < particles.Length; i++ )
            {
                Particle p = particles[i];
                if ( Math.Sqrt( ( p.X - mean_x ) * ( p.X - mean_x ) - ( p.Y - mean_y ) * ( p.Y - mean_y ) ) < 1 )
                {
                    mCount++;
                }
            }
            if ( mCount > particles.Length * 0.95 )
            {
                isDone = true;
            }

            meanX = mean_x;
            meanY = mean_y;
        }


    }
}
